#include "my_flash.h"
#include "my_uart.h"
#include "sys_param.h"
#include "my_err_check.h"


SYS_FLASH_PARAM_t flash_param;


#define FLASH_PROTECTED_PAGES       FLASH_WRP_Pages62to63 

#define FLASH_ONE_PAGE_SIZE         (2048)

static int test_dataaa;

/**
* @brief  Receive a file using the ymodem protocol
* @param  buf: Address of the first byte, 单次写入4个字节
* @retval The size of the file
*
*
*FLASH_WRP_Pages12to13
*/
void my_flash_param_read(void)
{
    /* Unlocks the FLASH Program Erase Controller */
    flash_param.bat_low_notify_flag = (*(__IO uint32_t*)(USER_PARAM_FLASH_ADDRESS ));
    flash_param.bat_low_power_off_flag = (*(__IO uint32_t*)(USER_PARAM_FLASH_ADDRESS + 4));

   /* Locks the FLASH Program Erase Controller */
    printf("Flash read data[%d]:[%d] \r\n", flash_param.bat_low_notify_flag, flash_param.bat_low_power_off_flag);
}


/**
* @brief  Receive a file using the ymodem protocol
* @param  buf: Address of the first byte, 单次写入4个字节
* @retval The size of the file
*
*
*FLASH_WRP_Pages12to13
*/
int my_flash_write_32_data(uint32_t *p_Address, uint32_t* p_Data, uint32_t len)
{
  int i;
  
  for(i = 0; i < len; i++){
    // **************如果地址是新page页，则先擦除page
    if((*p_Address)%FLASH_ONE_PAGE_SIZE == 0){
      FLASH_Unlock();
      FLASH_EraseOnePage((*p_Address));
    }
  
    // 写入4个字节的数据
    FLASH_ProgramWord((*p_Address), *p_Data);
    // 一次写入4个byte后，地址向后便宜4byte
    *p_Address += 4;
    p_Data ++;
    test_dataaa = *p_Data;
   
  }
    return 0;
}
//***************************************************
// flash 存储
//   3.8v以上，清楚低电标志位，低于3.8v则保留低电位
//***************************************************
void my_flash_param_write(void)
{
    int tmp_addr, i;
    int write_data_flag = 0;
    uint32_t WRPR_Value = 0xFFFFFFFF, ProtectedPages = 0, ProgramData = 0xFFFFFF;
    
    // my_flash_param_read();
    // test---
    if((get_sys_err_mode())& BAT_LOWEST){
        flash_param.bat_low_power_off_flag = 1;
        write_data_flag = 1;
        printf("Flash bat_lowest \r\n");
    }
    else if((get_sys_err_mode())& BAT_LOW){
        flash_param.bat_low_notify_flag = 1;
        flash_param.bat_low_power_off_flag = 0;
        write_data_flag = 1;
        printf("Flash bat_lowe_notify \r\n");
    }
    else if(get_sys_battery_voltage() > BACK_TO_MORMAL_BAT_VAL){
        if((flash_param.bat_low_notify_flag)||(flash_param.bat_low_power_off_flag)){
            write_data_flag = 1;
        }

        flash_param.bat_low_notify_flag = 0;
        flash_param.bat_low_power_off_flag = 0;
        printf("Flash bat_flag change to normal \r\n");
    }

    if(0 == write_data_flag){
        printf("Flash flag_no_change [%d]:[%d] \r\n", flash_param.bat_low_notify_flag, flash_param.bat_low_power_off_flag);
        return;
    }
    //---------------------------------   
    /* Program FLASH */
    /* Configures the Internal High Speed oscillator */
    if(FLASH_HSICLOCK_DISABLE == FLASH_ClockInit())
    {
        printf("HSI oscillator not yet ready\r\n");
        while(1);
    }

    /* Unlocks the FLASH Program Erase Controller */
    FLASH_Unlock();

    /* Erase */
    if (FLASH_COMPL != FLASH_EraseOnePage(USER_PARAM_FLASH_ADDRESS))
    {
        while(1)
        {
            printf("Flash EraseOnePage Error. Please Deal With This Error Promptly\r\n");
        }
    }
 
    tmp_addr = USER_PARAM_FLASH_ADDRESS;
    my_flash_write_32_data(&tmp_addr, (uint32_t*)&flash_param, 2);
    printf("Flash write notify:[%d]--power:[%d] \r\n", flash_param.bat_low_notify_flag, flash_param.bat_low_power_off_flag);
    FLASH_Lock();
   
    //---------------------------------     
}


//***************************************************
// 要在按键长按后，读取
// 3.8v以上，清楚低电标志位，低于3.8v则保留低电位
//***************************************************
void my_flash_data_init(void)
{

    my_flash_param_read();
    if(get_sys_battery_voltage() < BACK_TO_MORMAL_BAT_VAL)
    {
        if(flash_param.bat_low_power_off_flag == 1){
            set_sys_err_mode(BAT_LOWEST);
            set_sys_state(ERR_STA);
            set_sys_bat_lowest_flag(1);
            printf(" enter ERR_STA\r\n");
        }
        else if(flash_param.bat_low_notify_flag == 1){
            set_sys_err_mode(BAT_LOW);
        }
    }
}

